package com.sgb.kjwl.view.ui.fragment.shop;

import android.content.Context;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityShopCompanyBinding;
import com.sgb.kjwl.model.entity.ShopInfoEntity;
import com.sgb.kjwl.view.ui.activity.self.shop.CompanyShopInfoActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopInfoActivity;
import com.sgb.kjwl.view.ui.adapter.shopping.ShopCompanyImgAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MLog;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

import static androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL;

/**
 * Author chenfangkai
 */
public class ShopCompanyInfoFragment extends AppFragment {
    public static final String KEY_TYPE = "type";
    private ShopInfoActivity activity;
    private boolean isShow = false;
    private ActivityShopCompanyBinding binding;
    private ShopCompanyImgAdapter adapter;
    private ShopCompanyImgAdapter adapterCertificate;
    private ShopInfoEntity shopInfoEntity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ShopInfoActivity) {
            activity = (ShopInfoActivity) context;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.activity_shop_company, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        initView();
    }

    public static ShopCompanyInfoFragment newInstance(String type, ShopInfoEntity shopInfoEntity) {
        ShopCompanyInfoFragment fragment = new ShopCompanyInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        bundle.putSerializable("shopInfoEntity", shopInfoEntity);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initView() {
        binding.setViewModel(shopInfoEntity);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("店铺详情");
        binding.include.topLayout.setVisibility(View.GONE);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        GridLayoutManager layoutManagerCertificate = new GridLayoutManager(getActivity(), 2);
        if (adapter == null) {
            adapter = new ShopCompanyImgAdapter(getActivity(), null);
        }
        binding.companyImgRv.setLayoutManager(layoutManager);
        binding.companyImgRv.setAdapter(adapter);

        if (adapterCertificate == null) {
            adapterCertificate = new ShopCompanyImgAdapter(getActivity(), null);
        }
        binding.certificateRv.setLayoutManager(layoutManagerCertificate);
        binding.certificateRv.setAdapter(adapterCertificate);
        adapter.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
//                CompanyShopInfoActivity.start(getActivity(), "");
            }
        });
        binding.showTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isShow) {
                    binding.contentTv.setMaxLines(10000);
                    binding.showTv.setText("收起");
                    isShow = true;
                } else {
                    binding.contentTv.setMaxLines(3);
                    binding.showTv.setText("展开");
                    isShow = false;
                }

            }
        });
        Document doc = Jsoup.parseBodyFragment(shopInfoEntity.getIntroduction());
        MLog.d("", shopInfoEntity.getIntroduction());
        Elements elements = doc.select("img");
        for (Element each : elements) {
            each.attr("style", "width:100%");
        }

        String html = doc.html();
        Document htmlResult = Jsoup.parseBodyFragment(html);
        elements = htmlResult.select("body");
        for (Element each : elements) {
            each.attr("style", "font-size:34px");
        }


        MLog.d("", htmlResult.html());

        binding.videoView.setUp(shopInfoEntity.getVideoUrl(), JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "企业视频");
        binding.webView.loadData(htmlResult.html(), "text/html; charset=UTF-8", null);
        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setSupportZoom(false);
        binding.webView.getSettings().setBuiltInZoomControls(false);
        binding.webView.getSettings().setUseWideViewPort(true);
        binding.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        binding.webView.getSettings().setLoadWithOverviewMode(true);
        binding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                binding.webView.loadUrl("javascript:document.body.style.padding=\"6.5%\"; void 0");
            }
        });

        if (shopInfoEntity.getImage().size()>0) {
            adapter.setDatas(shopInfoEntity.getImage());
        } else {
            adapter.setDatas(null);
        }
        if (!"".equals(shopInfoEntity.getRelevantCertificate())) {
            List<String> certificate = new ArrayList<>(Arrays.asList(Objects.requireNonNull(shopInfoEntity.getRelevantCertificate().split(","))));
            adapterCertificate.setDatas(certificate);
        } else {
            adapterCertificate.setDatas(null);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.videoView.setFocusable(false);
    }

    private void init() {
        shopInfoEntity = (ShopInfoEntity) getArguments().getSerializable("shopInfoEntity");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.call_phone_img:
                CompanyShopInfoActivity.start(getContext(), "");
                break;
        }
    }
}
